package net.dodoman;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class TestMD5 {

	public static String MD5(byte[] s) {
		// 16进制字符
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'a', 'b', 'c', 'd', 'e', 'f' };
		try {
			byte[] strTemp = s;
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(strTemp);
			byte[] md = mdTemp.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			// 移位 输出字符串
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			return null;
		}
	}

	public static void main(String[] args) throws NoSuchAlgorithmException {
		// 计算运行需要多长时间
		long t1 = System.currentTimeMillis();
		System.out.println(t1);
		// 得到文件长度
		File file = new File("E:\\1.bmp");
		byte[] b = new byte[(int) file.length()];
		System.out.println(file.length());
		System.out.println(System.currentTimeMillis());
		try {
			InputStream in = new FileInputStream("E:\\1.bmp");
			in.read(b);
			System.out.println(in.read());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		System.out.println(TestMD5.MD5(b));

		long t2 = System.currentTimeMillis();
		System.out.println(t2);

		System.out.println(t2 - t1);
	}
}
